<?php

class Login extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

	/**default function
	 * loads page for login
	 * @param error: potential errors from previous attempts*/
	function index($error ='') {

		$params['subview'] = 'subview/login_view';
		if($error != '')
		$params['login_error'] = $error;
		$this->load->view('homepage_view', $params);

	}

	/**Function for account registration page
	 *
	 * @param error: message from previous attempts to be displayed*/
	function register($error='') {

		$params['subview'] = 'subview/register_view';
		//if there were database errors from last time
		if($error != '')
		$params['db_reg_error'] = $error;
		//load view
		$this->load->view('homepage_view', $params);

	}

	/**
	 * performs login validation
	 * logs the user in on success or prints error message if not
	 *
	 */
	function validate() {
		$this->load->model('gateway_model');

		$form_input = array(
			'account' => $this->input->post('account'),
			'password' => $this->input->post('password')
		);
		$account = $this->gateway_model->validate($form_input);

		if(isset($account)){
			//$this->session->unset_all_userdata();
			//Will use profile model to get data about user logging in
			$this->load->model('profile_model');
			//$this->load->model('ladder_model');

			//Set session variable to store user name and the fact that he is logged in.
			$data = array(
				'account' => $account,
				'logged_in' => true,
				'profile_data' => $this->profile_model->get_data($account)
			);
			//clear data from non logged in activity


			//Determines if the session should be remembered or not
			if ($this->input->post('remember') != 'accept'){
				$this->config->set_item('sess_expire_on_close', TRUE);
			} else {
				$this->config->set_item('sess_expire_on_close', FALSE);
			}

			$this->session->set_userdata($data);
			redirect('profile/show/' .$data['account']);
		}
		else{
			$this->index('Invalid username or password.');
		}
	}

	/**
	 * Function for account creation form
	 * validates form, loops while not valid,
	 * */
	function create_account() {
		$this->load->library('form_validation');

		//Enforce rules of form fields.
		$this->form_validation->set_rules('account', 'Account', 'trim|required|min_length[3]|alpha_dash');
		$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
		$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'trim|required|matches[password]');
		$this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
		$this->form_validation->set_rules('bnet', 'Battle.net URL', 'trim');
		$this->form_validation->set_rules('birthday', 'Birthday (yyyy-mm-dd)', 'trim');


		if($this->form_validation->run()){
			//Validation successful, attempt to create account
			$this->load->model('gateway_model');

			//get data from form
			$insert_data = array(
				'account' => $this->input->post('account'),
				'first_name' => $this->input->post('first_name'),
				'last_name' => $this->input->post('last_name'),
				'email' => $this->input->post('email'),
				'bnet' => $this->input->post('bnet'),
				'birthday' => $this->input->post('birthday'),
				'password' => $this->input->post('password')
			);

			$query = $this->gateway_model->create_account($insert_data);
			if($query){
				//Load some success message
				$params['subview'] = 'subview/register_success_view';
				$this->load->view('homepage_view', $params);
			}
			else{

				$this->register($this->db->_error_message());
			}
		}
		else{
			$this->register();
		}

	}

	/**function for logout link.
	 * logs the user out, unsets sessoin data
	 * and redirects to homepage*/
	function logout() {
		$this->session->unset_all_userdata();
		redirect('homepage');
	}

}
